package com.by.yz.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.android.volley.toolbox.NetworkImageView;
import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.common.MFGT;
import com.by.yz.entity.Account;
import com.by.yz.entity.FeedBack;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.FeedBackRESTFul;
import com.by.yz.util.ToJSonObject;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 123 on 2016/10/7.
 */
public class FeedbackActivity extends BaseActivity implements View.OnClickListener{

    private LinearLayout feedback;
    private EditText feedbackText;
    private Button commit;
    private  String content;
    private Toast toast;
    FeedBack fb,fb1;
    Account account;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_feedback_activity);

        initView();
        initDatas();

    }

    private void initDatas() {

    }

    private void initView() {
        setToolbarTitle("意见反馈");
        feedback=(LinearLayout) findViewById(R.id.feedback);
        feedback.setOnClickListener(this);

        feedbackText=(EditText) findViewById(R.id.feedback_text);
        feedbackText.setOnClickListener(this);

        commit=(Button) findViewById(R.id.commit);
        commit.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.feedback:
                openSoftInput();
                break;

            case R.id.commit:
                content = feedbackText.getText().toString();
                if(content.equals("")){
                    if(toast != null){
                        toast.setText("文本不能为空");
                        toast.setDuration(Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
                        toast.show();
                    }
                    else{
                        toast = Toast.makeText(this , "文本不能为空", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
                        toast.show();
                    }
                }else {

                    AccountRESTFul accountRESTFul = new AccountRESTFul();
                    accountRESTFul.get(new AccountRESTFul.IhttpCb() {
                        @Override
                        public void ok(JSONObject response) {
                            account = ToJSonObject.jsonToBean(response.toString(), Account.class);
                            fb=new FeedBack(null,content,account.getLogin());
//                            Log.e("fbLogin", fb.getLogin());
//                            Log.e("fbContent", fb.getContent());
                            JSONObject data = ToJSonObject.beanToJSONObejct(fb);

                            FeedBackRESTFul fbRESTFul = new FeedBackRESTFul();
                            fbRESTFul.postFeedback(data, new FeedBackRESTFul.MyPostCallBack() {
                                @Override
                                public void ok(JSONObject response) throws JSONException {
                                    Log.e("fbSuccess", "111");
                                    Toast.makeText(getApplicationContext(), "提交成功感谢您的宝贵意见我们会继续努力", Toast.LENGTH_SHORT).show();
                                    FeedbackActivity.this.finish();
                                }

                                @Override
                                public void error(String error) {
                                    Log.e("fbError", "222");
                                }
                            });


                        }

                        @Override
                        public void error(String error) {
                            Log.e("FBA-account-error", "授权失败" + error);
                        }
                    });
                    break;
                }

        }
    }

    private void openSoftInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(feedbackText,InputMethodManager.SHOW_FORCED);
    }

}
